﻿using B.S.DataBase.MyProject.ErrorCode;
using B.S.Traceability.Domain;
using B.S.Traceability.Infrastructure.Interface;
using B.S.Traceability.Write.API.Applicantion.Command.HealthCommand;
using MediatR;

namespace B.S.Traceability.Write.API.Applicantion.CommandHandler.HealthCommandHandler
{
    public class DelHealthCommandHandler : IRequestHandler<DelHealthCommand, APIResult<int>>
    {
        private readonly IBaseRepository<SU_Health> healthRep;
        public DelHealthCommandHandler(IBaseRepository<SU_Health> healthRep)
        {
            this.healthRep = healthRep;
        }
        public async Task<APIResult<int>> Handle(DelHealthCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var healthlist = healthRep.GetAll().Result.FirstOrDefault(x => x.HealthId == request.HealthId);
            healthlist.IsDel=true;
            var res= healthRep.Update(healthlist).Result;
            if (res>0)
            {
                result.Code = APIEnum.删除成功;
                result.Msg = APIEnum.删除成功.ToString();
                result.data= res;
            }
            else
            {
                result.Code = APIEnum.删除失败;
                result.Msg = APIEnum.删除失败.ToString();
                result.data = res;
            }
            
            return await Task.FromResult(result);
        }
    }
}
